// #ifdef APP-PLUS
import permision from "@/js_sdk/wa-permission/permission.js";
// #endif
const permissionConfig={
	// 检查定位权限
	'location':{
		'text':'地理位置',
		'mp':'scope.userLocation',
		'iOS':'location',
		'Android':'android.permission.ACCESS_FINE_LOCATION',
	},
	// 检查摄像头权限
	'camera':{
		'text':'相机',
		'mp':'scope.camera',
		'iOS':'camera',
		'Android':'android.permission.CAMERA',
	},
	// 检查相册读取权限
	'photoLibraryRead':{
		'text':'相册读取',
		'mp':'scope.camera',
		'iOS':'photoLibrary',
		'Android':'android.permission.READ_EXTERNAL_STORAGE',
	},
	// 检查相册写入权限
	'photoLibraryRead':{
		'text':'保存到相册',
		'mp':'scope.writePhotosAlbum',
		'iOS':'photoLibrary',
		'Android':'android.permission.WRITE_EXTERNAL_STORAGE',
	},
	// 检查拨打电话权限
	'phoneCall':{
		'text':'拨打电话',
		'mp':'',
		'iOS':'',
		'Android':'android.permission.CALL_PHONE',
	},
};
/**
 * 检查权限
 * @param {*} type 
 * @param {*} cb 
 */
function checkPermission(type,cb){
	// #ifdef APP-PLUS
	var permisionID=permissionConfig[type][plus.os.name];
	var text=permissionConfig[type]['text'];
	if (plus.os.name == 'iOS') {
		cb();
		// if(permision.judgeIosPermission(permisionID)){
		// 	cb();
		// }else{
		// 	gotoAppPermissionSetting(text);
		// }
	}else{
		requestAndroidPermission(permisionID,text,cb);
	}
	return false;
	// #endif
	// #ifdef MP
	uni.getSetting({
		success: function(res) {
			if (res.authSetting[permissionConfig[type]['mp']] == false) {
				// 拒绝授权，弹窗提示
				uni.showModal({
					title: '提示',
					content: '您已拒绝授权，无法获取' + permissionConfig[type]['text'] + '，是否立即设置',
					success: function(res) {
						if (res.confirm) {
							uni.openSetting({
								fail: function(e) {
									console.log(e.errMsg);
								}
							});
						}
					}
				});
			} else {
				cb();
			}
		}
	});
	// #endif
}
function gotoAppPermissionSetting(text){
	uni.showModal({
			title: '提示',
			content: '您已拒绝授权，无法获取' + text+ '的权限，是否立即设置',
			success: function(res) {
				if (res.confirm) {
					permision.gotoAppPermissionSetting()
				}
			}
		});
	
}
async function requestAndroidPermission(permisionID,text,cb) {
	var result = await permision.requestAndroidPermission(permisionID)
	if(result==1){
		cb();
	}else{
		gotoAppPermissionSetting(text);
	}
}
module.exports = {
	checkPermission:checkPermission,
};